package com.fly.files.list;

import java.io.IOException;
import java.util.Arrays;
import java.util.stream.Stream;

import org.junit.jupiter.api.Test;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class FileListBySpring
{
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    
    String userProfile = "file:" + System.getenv().get("USERPROFILE");
    
    /**
     * 遍历文件
     * 
     * @throws IOException
     */
    @Test
    public void testList()
        throws IOException
    {
        // 外部文件
        Resource[] jpgs = resolver.getResources(userProfile + "/Pictures/**/*.jpg");
        Arrays.stream(jpgs).forEach(System.out::println);
        
        // 外部文件
        Resource[] pngs = resolver.getResources(userProfile + "/Pictures/**/*.png");
        Arrays.stream(pngs).forEach(System.out::println);
        
        // 工程或jar内文件
        Resource[] xmls = resolver.getResources("classpath*:**/*.xml");
        Arrays.stream(xmls).forEach(System.out::println);
        
        // 合并
        log.info("################################################## 合并后 ##################################################");
        Stream.of(jpgs, pngs, xmls).flatMap(Arrays::stream).forEach(System.out::println);
    }
    
    /**
     * 遍历文件，支持指定文件名搜索
     * 
     * @throws IOException
     */
    @Test
    public void testList2()
        throws IOException
    {
        Resource[] pngs = resolver.getResources(userProfile + "/Pictures/**/001.png");
        Arrays.stream(pngs).forEach(System.out::println);
        
        Resource[] pngs2 = resolver.getResources(userProfile + "/Pictures/**/00*.PNG");
        Arrays.stream(pngs2).forEach(System.out::println);
        
        Resource[] xmls = resolver.getResources("file:C:/Gitee/00fly/effict-side/**/pom.xml");
        Arrays.stream(xmls).forEach(System.out::println);
    }
    
    /**
     * 遍历文件，支持指定文件(包含目录)名搜索
     * 
     * @throws IOException
     */
    @Test
    public void testList3()
        throws IOException
    {
        String userProfile = "file:C:/gitee/00fly/effict-side/";
        Resource[] pngs = resolver.getResources(userProfile + "**/src");
        Arrays.stream(pngs).forEach(System.out::println);
    }
}
